package com.dckeji.shiplock.webapp.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/10
 * Time: 18:52
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Object manager = request.getSession().getAttribute("login");
        if (request.getRequestURI().equals("/rest/ma/login")) {
            filterChain.doFilter(request, servletResponse);
        } else {
            if (null != manager) {
                filterChain.doFilter(request, servletResponse);
            } else {
                request.getRequestDispatcher("/login.do").forward(request, servletResponse);
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {

    }
}
